// =======================================================================
// 
//       Filename:  process.hpp
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  03.05.2010 17:59:52
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Dominik 'domachine' Burgdörfer (-), dominik.burgdoerfer@googlemail.com
//        Company:  -
// 
// =======================================================================


#ifndef  PROCESS_INC
#define  PROCESS_INC
#include <iostream>
#include <cstring>

namespace webplodder {
    namespace process {
        /**
         * @brief Interface to spawn a system process
         * and communicate with it.
         */
        class Process {
        public:
            /**
             * @brief Constructs a Process object.
             * @param command The system command
             */
            Process(const std::string& command);

            /**
             * @brief Spawns the process and opens read
             * and write channel.
             */
            pid_t launch();

            /**
             * @brief Reads one character from the process
             * output and stores it in <i>ch</i>.
             * @param[out] ch The read character will be stored in it
             */
            bool operator>>(char& ch);

            /**
             * @brief Writes <i>ch</ch> to the process input device.
             * @param[in] ch The character to write
             */
            Process& operator<<(char ch);

            /**
             * @brief Closes read channel.
             */
            void closeReadChannel();

            /**
             * @brief Closes write channel.
             */
            void closeWriteChannel();

            /**
             * @brief Closes read and write channel.
             */
            void close();

        private:
            std::string m_command;
            int m_fileDescriptors[2];
        };
    }
}
#endif   // ----- #ifndef PROCESS_INC  -----
